package mercurius.fitness.client.rb.exercise;

import mercurius.fitness.client.Fitness;
import mercurius.fitness.client.locale.Locale;
import mercurius.fitness.client.rb.exercise.forms.AddExerciseForm;
import mercurius.fitness.client.rb.exercise.forms.EditExerciseForm;
import mercurius.fitness.client.rb.exercise.forms.ViewExerciseForm;
import mercurius.fitness.client.ui.IFrame;
import net.pleso.framework.client.bl.IAuthDataSource;
import net.pleso.framework.client.bl.actions.IDeleteRowExecutor;
import net.pleso.framework.client.bl.forms.IAddForm;
import net.pleso.framework.client.bl.forms.IEditForm;
import net.pleso.framework.client.bl.forms.IViewForm;
import net.pleso.framework.client.bl.providers.IActionProvider;
import net.pleso.framework.client.bl.providers.IActionProviders;
import net.pleso.framework.client.bl.providers.IAddFormProvider;
import net.pleso.framework.client.bl.providers.IDeleteRowProvider;
import net.pleso.framework.client.bl.providers.IEditFormProvider;
import net.pleso.framework.client.bl.providers.IViewFormProvider;
import net.pleso.framework.client.bl.rb.IRB;
import net.pleso.framework.client.bl.rb.columns.IRBColumn;
import net.pleso.framework.client.bl.rb.columns.impl.RBColumn;
import net.pleso.framework.client.dal.IDataRow;
import net.pleso.framework.client.dal.SelectParams;
import net.pleso.framework.client.util.DateUtil;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class ExerciseRB implements IRB, IActionProviders {
	
	
	
	private class AddRowAction implements IAddFormProvider {

		public IAddForm getAddForm(IDataRow row) {
			return new AddExerciseForm();
		}

		public String getActionCaption() {
			return null;
		}
		
	}
	
	private class EditRowAction implements IEditFormProvider {

		public IEditForm getEditForm(IDataRow row) {
			if (row instanceof Exercise && row != null)
				return new EditExerciseForm(((Exercise)row).getID());
			else
				return new EditExerciseForm(null);
		}

		public String getActionCaption() {
			return null;
		}
		
	}
	
	private class ViewRowAction implements IViewFormProvider {

		public IViewForm getViewForm(IDataRow row) {
			
			if (row instanceof Exercise && row != null) {
				Exercise ex = ((Exercise)row);
				if(ex.isGPS()) {
					String url = Fitness.get().getURL()+"?action=generateGMap&embed=true&id=" + ex.getID().intValue();
					if(Fitness.get().getFeedKey().length() > 0) {
						url += "&feedkey=" + Fitness.get().getFeedKey();
					}
					new IFrame(DateUtil.formatDate(ex.getDate()) + " GPS", url).show();
				}
				
				return new ViewExerciseForm(((Exercise)row).getID());
			}
			else
				return new ViewExerciseForm(null);
		}

		public String getActionCaption() {
			return null;
		}
		
	}

	private class DeleteRowAction implements IDeleteRowProvider, IDeleteRowExecutor {

		public IDeleteRowExecutor getDeleteRowExecutor() {
			return this;
		}

		public String getActionCaption() {
			return null;
		}

		public void deleteRow(IDataRow row, AsyncCallback callback) {
			dataSet.delete(((Exercise) row).getID(), callback); 			
		}
		
	}

	private static ExerciseDataSet dataSet = new ExerciseDataSet();
	
	public static ExerciseDataSet getDataSet() {
		return dataSet;
	}
	
	private class RBDataSource implements IAuthDataSource {
		
		public void select(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.select(params, callback);
		}

		public void selectCount(SelectParams params, AsyncCallback callback) {
			params.setLimit(Fitness.get().getDefaultRowLimit());
			dataSet.selectCount(callback);
		}
	}

	public String getCaption() {
		return Locale.constants().exercise_rb_caption();
	}

	private static RBColumn[] rbColumns = new RBColumn[] {
	//	new RBColumn(Exercise.Columns.id, 10),
		new RBColumn(Exercise.Columns.date, 10),
		new RBColumn(Exercise.Columns.duration, 10),
		new RBColumn(Exercise.Columns.points, 10),
		new RBColumn(Exercise.Columns.calories, 10),
		new RBColumn(Exercise.Columns.distance, 10),
	//	new RBColumn(Exercise.Columns.distance_unit, 10),
		new RBColumn(Exercise.Columns.activity, 10),
	//	new RBColumn(Exercise.Columns.notes, 30)
			

		/*

		new RBColumn(Exercise.Columns.distance, 30),
		new RBColumn(Exercise.Columns.calories, 30),
		new RBColumn(Exercise.Columns.distance_unit, 30)
		new RBColumn(Exercise.Columns.avghr, 30),
		new RBColumn(Exercise.Columns.maxhr, 30)
		new RBColumn(Exercise.Columns.avgspeed, 30),
		new RBColumn(Exercise.Columns.bestspeed, 30)
		new RBColumn(Exercise.Columns.avgpace, 30),
		new RBColumn(Exercise.Columns.bestpace, 30)
		new RBColumn(Exercise.Columns.speedunit, 30),
		new RBColumn(Exercise.Columns.notes, 30)
		new RBColumn(Exercise.Columns.title, 30),
		new RBColumn(Exercise.Columns.location, 30)
		new RBColumn(Exercise.Columns.pub, 30)
		
		
		 */
	};

	public IRBColumn[] getColumns() {
		return rbColumns;
	}

	public IAuthDataSource getDataSource() {
		return new RBDataSource();
	}

	public IActionProvider[] getActionProviders() {
		return new IActionProvider[] {
			//	new AddRowAction(),
				new ViewRowAction(),
				new DeleteRowAction(),
				new EditRowAction()
		};
	}

}
